package com.hdaccp.goodsprovider.controller;

import com.hdaccp.goodsprovider.dao.GoodsTypeDao;
import com.hdaccp.goodsprovider.service.GoodsTypeService;
import com.sun.org.apache.regexp.internal.RE;
import org.main.bean.POJO.PGoodsType;
import org.main.bean.POJO.PType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/goodstype")
public class GoodsTypeController {
    @Autowired
    private GoodsTypeDao goodsTypeDao;
    @Autowired
    private GoodsTypeService goodsTypeService;

    @RequestMapping("/queryPage/{currentage}/{pageSize}")
    public List<PGoodsType>queryPage(@PathVariable("currentage") int currentage,@PathVariable("pageSize") int pageSize){
        return goodsTypeService.queryPage(currentage,pageSize);
    }
    @RequestMapping("/query")
    public List<PGoodsType>query(){
        return goodsTypeDao.findAll();
    }

   //根据id查询
    @RequestMapping("/getOneId/{id}")
    public PGoodsType getOneId(@PathVariable("id") int id){
        return goodsTypeDao.getOne(id);
    }

    //增加

    @RequestMapping("/save/{id}/{brand}/{sizeId}")
    public String save(@PathVariable("id") int id,@PathVariable("brand") String brand,@PathVariable("sizeId") int sizeId){
        String msg="增加商品品牌成功";
        PGoodsType pGoodsType=new PGoodsType();
        pGoodsType.setId(id);
        pGoodsType.setBrand(brand);
        PType pType=new PType();
        pType.setId(sizeId);
        pGoodsType.setPType(pType);

        try {
            goodsTypeDao.save(pGoodsType);
        }catch (Exception e){
            msg="增加商品品牌失败";
        }

        return msg;
    }
    @RequestMapping("/update/{id}/{brand}/{sizeId}")
    public String update(@PathVariable("id") int id,@PathVariable("brand") String brand,@PathVariable("sizeId") int sizeId){
        String msg="修改商品品牌成功";
        PGoodsType pGoodsType=goodsTypeDao.getOne(id);
        pGoodsType.setBrand(brand);
        PType pType=new PType();
        pType.setId(sizeId);
        pGoodsType.setPType(pType);

        try{
            goodsTypeDao.save(pGoodsType);
        }catch(Exception e){
            msg="修改商品品牌失败";
        }
        return msg;
    }

    @RequestMapping("/del/{id}")
    public String del(@PathVariable("id") int id){
        String msg="删除商品品牌成功";

        try {
            goodsTypeDao.getOne(id);
        }catch (Exception e){
            msg="删除商品品牌失败";
        }
      return msg;
    }

}
